//
//  ProfileInsertViewController.swift
//  placepic
//
//  Created by elesahich on 2020/09/05.
//  Copyright © 2020 elesahich. All rights reserved.
//

import UIKit

class ProfileInsertViewController: UIViewController {
    
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var nameTextField: UITextField!
    @IBOutlet weak var explainLabel: UILabel!
    @IBOutlet weak var affiliationLabel: UILabel!
    @IBOutlet weak var affiliationTextField: UITextField!
    @IBOutlet weak var startButton: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMainView()
        setupNavigationBar()
        addTargetToTextFields()
    }
    @IBAction func nextButtonAction(_ sender: Any) {
        
    }
}

extension ProfileInsertViewController {
    
    private func addTargetToTextFields() {
        nameTextField.addTarget(self,
                                action: #selector(changeFirstTextFieldActions),
                                for: .allEditingEvents)
        
        affiliationTextField.addTarget(self,
                                       action: #selector(changeButtonActions),
                                       for: .allEditingEvents)
    }
    
    @objc
    func changeFirstTextFieldActions() {
        if nameTextField.hasText {
            nameTextField.layer.borderColor = UIColor.gray70.cgColor
            nameTextField.layer.borderWidth = 1.5
            nameTextField.backgroundColor = UIColor.white
        } else {
            nameTextField.backgroundColor = UIColor.blueGray10
            nameTextField.layer.borderColor = UIColor.blueGray10.cgColor
        }
    }
    
    @objc
    func changeButtonActions() {
        if affiliationTextField.hasText && nameTextField.hasText {
            affiliationTextField.layer.borderWidth = 1.5
            affiliationTextField.layer.borderColor = UIColor.gray70.cgColor
            affiliationTextField.backgroundColor = UIColor.white
            startButton.backgroundColor = UIColor.warmPink
            startButton.setTitleColor(UIColor.white, for: .normal)
        } else if affiliationTextField.hasText {
            affiliationTextField.layer.borderWidth = 1.5
            affiliationTextField.layer.borderColor = UIColor.gray70.cgColor
            affiliationTextField.backgroundColor = UIColor.white
            startButton.backgroundColor = UIColor.gray30
            startButton.setTitleColor(UIColor.gray50, for: .normal)
        } else if !affiliationTextField.hasText {
            affiliationTextField.layer.borderWidth = 1.5
            affiliationTextField.layer.borderColor = UIColor.blueGray10.cgColor
            affiliationTextField.backgroundColor = UIColor.blueGray10
            startButton.backgroundColor = UIColor.gray30
            startButton.setTitleColor(UIColor.gray50, for: .normal)
        }
    }
    
    private func setupMainView() {
        nameLabel.text = "이름"
        nameLabel.font = UIFont.boldSystemFont(ofSize: 15)
        nameLabel.textColor = UIColor.gray90
        
        let attributedString = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 15),
                                NSAttributedString.Key.foregroundColor: UIColor.blueGray30,
        ]
        nameTextField.setLeftPaddingPoints(10)
        nameTextField.attributedPlaceholder = NSAttributedString(string: "이름을 입력해주세요", attributes: attributedString)
        nameTextField.backgroundColor = UIColor.blueGray10
        nameTextField.layer.cornerRadius = 8
        nameTextField.layer.borderColor = UIColor.blueGray10.cgColor
        nameTextField.layer.borderWidth = 1
        nameTextField.clipsToBounds = true
        nameTextField.delegate = self
        
        explainLabel.font = UIFont.systemFont(ofSize: 9)
        explainLabel.textColor = UIColor.blueGray80
        explainLabel.text = "실명으로 입력해주세요."
        
        // Second StackView
        affiliationLabel.text = "소속"
        affiliationLabel.font = UIFont.boldSystemFont(ofSize: 15)
        affiliationLabel.textColor = UIColor.gray90
        
        affiliationTextField.setLeftPaddingPoints(10)
        affiliationTextField.attributedPlaceholder = NSAttributedString(string: "ex) 27기 디자인파트", attributes: attributedString)
        affiliationTextField.backgroundColor = UIColor.blueGray10
        affiliationTextField.layer.cornerRadius = 8
        affiliationTextField.layer.borderColor = UIColor.blueGray10.cgColor
        affiliationTextField.layer.borderWidth = 1
        affiliationTextField.clipsToBounds = true
        affiliationTextField.delegate = self
        
        // Button
        startButton.layer.cornerRadius = 8
        startButton.backgroundColor = UIColor.gray30
        startButton.setTitleColor(UIColor.gray50, for: .normal)
        startButton.setTitle("진짜 시작하기", for: .normal)
        startButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 16)
    }
    
    private func setupNavigationBar() {
        guard let navigationBar = self.navigationController?.navigationBar else { return }
        
        navigationBar.isTranslucent = true
        navigationBar.backgroundColor = UIColor.clear
        navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        navigationBar.shadowImage = UIImage()
        
        let title: UILabel = {
            let label = UILabel()
            label.text = "프로필 입력"
            label.font = .boldSystemFont(ofSize: 18)
            label.textColor = UIColor.gray90
            return label
        }()
        
        let backButton: UIButton = {
            let bt = UIButton()
            bt.setImage(UIImage(named: "backArrowIc"), for: .normal)
            return bt
        }()
        
        backButton.addTarget(self,
                             action: #selector(dismissVC),
                             for: .touchUpInside)
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)
        navigationItem.titleView = title
    }
    
    @objc
    private func dismissVC() {
        self.navigationController?.popViewController(animated: true)
    }
}

extension ProfileInsertViewController: UITextFieldDelegate {
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if textField == nameTextField {
            textField.resignFirstResponder()
        } else {
            textField.resignFirstResponder()
        }
        return true
    }
}
